package www.rlicker.controller;

import java.io.File;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import www.rlicker.model.GuidePicture;
import www.rlicker.model.Ueditor;
import www.rlicker.mybatis.Criteria;
import www.rlicker.service.IGuidePictureService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.StringUtil;

@Controller
@RequestMapping(value = "/file")
public class UploadController extends BaseController{

	@Autowired
	private IGuidePictureService service;
	
	protected boolean verifyUploadFile(String name)
	{
		for (String ext : uploadExtension.split(","))
		{
			if (ext.charAt(0) != '.') ext = '.' + ext;
			if (name.endsWith(ext)) return true;
		}
		return false;
	}
	
	private String uploadExtension = "jpg,png,gif,bmp,jpeg";
	
	@ResponseBody
	@RequestMapping(value = "/upold", method = { RequestMethod.POST })
	public String up(@RequestParam("file") MultipartFile file,@RequestParam("orderBy") String orderBy,@RequestParam("id") String id,@RequestParam("type") String type,String imgType,HttpServletRequest request, HttpServletResponse resp) throws Exception {
		String msg=null;
		String dirs = "shanda";
        if (file.isEmpty()) {
            return msg;

        }else {
        	if(!verifyUploadFile(file.getOriginalFilename())){
        		msg = "1";
        		 return msg;
        	}
        	//String path = request.getServletContext().getRealPath("/static/image");
          	String fileName=file.getOriginalFilename();
        	String img=getSeqNum()+fileName.substring(fileName.lastIndexOf("."));
        	String path = "";
        	String realPath = request.getSession().getServletContext() .getRealPath("/");
        	String path2 = realPath.substring(0, realPath.lastIndexOf(System.getProperty("file.separator")));
			String p = realPath.substring(0, (path2.lastIndexOf(System.getProperty("file.separator")))) + System.getProperty("file.separator") + "htxFile" + System.getProperty("file.separator") + dirs;
			String acesP = "http://" + "39.108.81.229" + ":" + request.getServerPort() + System.getProperty("file.separator") + "htxFile" + System.getProperty("file.separator") + "shanda" + System.getProperty("file.separator") + img;
			String path1 = "";
			path1 = p + System.getProperty("file.separator");
			final File localFile = new File(path1);
			
			try {
				if (!localFile.exists()) {
					localFile.mkdirs();
				}
				file.transferTo(new File(path));
			} catch (Exception e) {
			}
        /*	File dir=new File(path);
        	if(!dir.exists()){
        	    dir.mkdirs();
        	}*/
        	FileOutputStream imgOut=new FileOutputStream(new File(localFile,img));//
        	imgOut.write(file.getBytes());//
        	imgOut.close();
        	msg = acesP;
        	if (StringUtil.isNullOrBlank(imgType)) {
            	if (StringUtil.isNullOrBlank(id)) {
            		GuidePicture record = new GuidePicture();
                	record.setId(getSeqNum());
                	record.setPictureUrl(img);
                	record.setType(Integer.parseInt(type));
                	record.setOrderBy(Integer.parseInt(orderBy));
                	service.insert(record);
    			}else {
    				Criteria<GuidePicture> param = new Criteria<GuidePicture>();
    				GuidePicture picture = new GuidePicture();
    				picture.setPictureUrl(img);
    				param.setRecord(picture);
    				param.addParam("id", id);
    				service.update(param);
    			}
			}
	  }
        return msg;
	}
	
		@RequestMapping(value="/videoUpload")
		@ResponseBody
	    public String videoUpload(@RequestParam("file") MultipartFile upfile,@RequestParam("id") String id,HttpServletRequest request) throws Exception {
	        String msg="";
	    	String dirs = "shanda";
	    	String fileName=upfile.getOriginalFilename();
	        Ueditor ueditor = new Ueditor();
	       	if(upfile.getSize()>1024*1024*30){
	         	ueditor.setState("0M");
	  	        return null;
	        }
	        if(fileName!=null&&fileName!=""){   
	        	String vide=getSeqNum()+fileName.substring(fileName.lastIndexOf("."));
	        	String path = "";
	        	String realPath = request.getSession().getServletContext() .getRealPath("/");
	        	String path2 = realPath.substring(0, realPath.lastIndexOf(System.getProperty("file.separator")));
				String p = realPath.substring(0, (path2.lastIndexOf(System.getProperty("file.separator")))) + System.getProperty("file.separator") + "htxFile" + System.getProperty("file.separator") + dirs;
				String acesP = "http://" + "39.108.81.229" + ":" + request.getServerPort() + System.getProperty("file.separator") + "htxFile" + System.getProperty("file.separator") + "shanda" + System.getProperty("file.separator") + vide;
				String path1 = "";
				path1 = p + System.getProperty("file.separator");
				final File localFile = new File(path1);
				try {
					if (!localFile.exists()) {
						localFile.mkdirs();
					}
					upfile.transferTo(new File(path));
				} catch (Exception e) {
				}
			 	FileOutputStream imgOut=new FileOutputStream(new File(localFile,vide));//
	        	imgOut.write(upfile.getBytes());//
	        	imgOut.close();
	        	msg = acesP;
	        	if (StringUtil.isNullOrBlank(id)) {
            		GuidePicture record = new GuidePicture();
                	record.setId(getSeqNum());
                	record.setPictureUrl(vide);
                	record.setType(2);
                	record.setOrderBy(0);
                	service.insert(record);
    			}else {
    				Criteria<GuidePicture> param = new Criteria<GuidePicture>();
    				GuidePicture picture = new GuidePicture();
    				picture.setPictureUrl(vide);
    				param.setRecord(picture);
    				param.addParam("id", id);
    				service.update(param);
    			}
	        }
	        return msg;
	    }
}
